package com.welsmann.app.start.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.welsmann.app.start.common.StartConst;
import com.welsmann.app.start.entity.Holiday;

@Controller
public class HolidayController extends BaseController {

	@RequestMapping("/holiday")
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView("holiday");
		List<Holiday> lstHoliday = super.holidayService.getAll();
		List<Map<String, String>> lstMap = new ArrayList<Map<String, String>>();
		if (lstHoliday != null && !lstHoliday.isEmpty()) {
			int year = Calendar.getInstance().get(Calendar.YEAR);
			year += year < 2013 ? 1900 : 0;
			for (Holiday h : lstHoliday) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("year", year + "");
				map.put("date", String.format("%s月%s日", h.getMonth() < 10 ? "0"
						+ h.getMonth() : h.getMonth(), h.getDay() < 10 ? "0"
						+ h.getDay() : h.getDay()));
				map.put("title", h.getTitle());
				map.put("type", super.getHolidayType(h.getType()));
				lstMap.add(map);
			}
		}
		mav.addObject(StartConst.REQUEST_HOLIDAY_LIST, lstMap);
		mav.addObject(StartConst.REQUEST_HOLIDAY_COUNT, lstMap.size());
		return mav;
	}
}
